local composer = require( "composer" )
local widget = require "widget"
local physics = require "physics"
local scene = composer.newScene() 


-- Variabili

local environment = 0
local bufala
local moveIndex=1
local y
local y1
local News
local TimerNews
local StarterLine
local Line
local prevX=W/13
local prevY=H/2
local randomX -- coordinata x della nuova linea
local randomY -- coordinata y della nuova linea
local A
local vectorA={}
local pcLineUp
local pcLineDown
local pcIndexVector={}
local boxUp
local boxDown
local boxVector={}
local varMovimento=1
local pcUp
local pcDown
local pcVector={
{file="Game/Pc/pcDetective.png",nome="detective"},
{file="Game/Pc/pcDonna.png",nome="donna"},
{file="Game/Pc/pcPolice.png",nome="police"},
{file="Game/Pc/pcRagazzo.png",nome="ragazzo"},
{file="Game/Pc/pcUomo.png",nome="uomo"},
}
local menuGroup=display.newGroup()
local menuRect
local convinci
local miPiace
local condividi
local credBar
local credText
local totalPoints
local actualPoints
local credVar=30
 
-- "scene:create()"
function scene:create( event )

local sceneGroup = self.view

-- genero i nodi della rete
A=0

for i=1,7 do

StarterLine=display.newLine(sceneGroup,W/9,H/2,W/4,H/2)
StarterLine.x=A
StarterLine:setStrokeColor(1,1,0)
StarterLine.strokeWidth=8
A=A+StarterLine.contentWidth
table.insert(vectorA,A)
for i,A in pairs(vectorA) do
	print(i,A)
end

-- generazione linee superiori e inferiori

pcLineUp = display.newLine(sceneGroup,vectorA[i],H/2,vectorA[i],H/4)
pcLineUp:setStrokeColor(1,1,0)
pcLineUp.strokeWidth=8
pcLineDown = display.newLine(sceneGroup,vectorA[i],H/2,vectorA[i],H/1.3)
pcLineDown:setStrokeColor(1,1,0)
pcLineDown.strokeWidth=8

--generazione pc

local indexUp=math.random(1,5)
pcUp=display.newImageRect(sceneGroup,pcVector[indexUp].file,W/7,H/6)
pcUp.x=vectorA[i]; pcUp.y=H/4
local indexDown=math.random(1,5)
pcDown=display.newImageRect(sceneGroup,pcVector[indexDown].file,W/7,H/6)
pcDown.x=vectorA[i]; pcDown.y=H/1.3

-- generazione blocchi "?"

boxUp=display.newImageRect(sceneGroup,"Game/puntointerr.png",W/7,H/6)
boxUp.x=vectorA[i]; boxUp.y=H/4

boxDown=display.newImageRect(sceneGroup,"Game/puntointerr.png",W/7,H/6)
boxDown.x=vectorA[i]; boxDown.y=H/1.3

table.insert(boxVector,boxUp)
table.insert(boxVector,boxDown)

end

-- genero la "bufala"

bufala=display.newImageRect(sceneGroup,"Game/StarterBuf.png",W/10,H/6)
bufala.x=W/15; bufala.y=H/2
bufala:toFront()

-- funzione per selezionare le opzioni del menu

local function convinciSelect()
	
	credVar=credVar+10
	menuGroup:removeSelf()
	actualPoints:removeSelf()
	actualPoints=display.newText(credVar,W/1.2,H/1.05,"showg.TTF",20)
	transition.to(bufala,{time=500,y=H/2,onComplete=moveOriz})
	--transition.to(bufala,{time=500,x=vectorA[moveIndex],onComplete=movementEnabled})

end

-- funzione per gestire il menu dei pc

local function openMenu()

	sceneGroup:insert(menuGroup)

	menuRect=display.newRoundedRect(W/2,H/1.98,W/3.5,H/3.3,12)
	menuRect:setFillColor(1,0,0)
	menuGroup:insert(menuRect)

	convinci=display.newText("convinci",W/2,H/2,"showg.TTF",35)
	convinci.x=menuRect.contentWidth*1.75; convinci.y=menuRect.contentHeight*1.4
	menuGroup:insert(convinci)
	convinci:addEventListener("tap",convinciSelect)

	miPiace=display.newText("Mi Piace",W/2,H/2,"showg.TTF",35)
	miPiace.x=menuRect.contentWidth*1.75; miPiace.y=menuRect.contentHeight*1.7
	menuGroup:insert(miPiace)

	condividi=display.newText("Condividi",W/2,H/2,"showg.TTF",35)
	condividi.x=menuRect.contentWidth*1.75; condividi.y=menuRect.contentHeight*2
	menuGroup:insert(condividi)

end

-- funzione per gestire i movimenti della bufala

local function bufalaMove(event)

	if event.phase=="began" then

		y=event.y

	elseif event.phase=="moved" then
		y1=event.y

			if y1<y then 

				transition.to(bufala,{time=500,y=H/4})
				bufala:removeEventListener("touch",bufalaMove)
				openMenu()

			elseif y1>y then

				transition.to(bufala,{time=500,y=H/1.3})
				bufala:removeEventListener("touch",bufalaMove)
				openMenu()
			end

	end

end

local function movementEnabled()

moveIndex=moveIndex+1
bufala:addEventListener("touch",bufalaMove)
boxVector[varMovimento]:removeSelf()
boxVector[varMovimento+1]:removeSelf()
varMovimento=varMovimento+1

end

local function moveOriz( )
	
	transition.to(bufala,{time=500,x=vectorA[moveIndex],onComplete=movementEnabled})

end
 
moveOriz()

	local NewsArray= {
	("Rivolte in piazza contro i vaccini"),
	("Medici smentiscono bufale sui vaccini"),
	("Attentato al presidente degli Stati Uniti"),
	("Belen Rodriguez vista a Roma con Nathan Gangi, è scandalo"),
	("Da domani whatsapp a pagamento, panico per le strade"),
	("Mark Zuckerberg trovato morto nel suo appartamento, azioni Facebook in declino"),
	}

		local function changeNews ()


			News=display.newText(NewsArray[math.random(1,6)],W/2,H/2,"showg.TTF",40)
			News.x=H/2.8; News.y=W/13

				local function removeNews()

					News:removeSelf()
					
				end


			transition.to(News,{time=8000,x=W/0.7,onComplete=removeNews})
			
		end

	
	TimerNews=timer.performWithDelay(20000,changeNews,0)


	-- creazione della barra credibilità

	credBar=display.newRoundedRect(W/1.2,H/1.07,W/6,H/8,12)
	credBar:setFillColor(1,0,0)
	credBar:setStrokeColor(1,1,0)
	credText=display.newText("Credibilità",W/1.2,H/1.1,"showg.TTF",20)
	totalPoints=display.newText("/100",W/1.15,H/1.05,"showg.TTF",20)
	actualPoints=display.newText(credVar,W/1.2,H/1.05,"showg.TTF",20)



	
end


-- "scene:show()"
function scene:show( event )
 
   local sceneGroup = self.view
   local phase = event.phase
    
   if ( phase == "will" ) then
      -- Called when the scene is still off screen (but is about to come on screen).
   elseif ( phase == "did" ) then
   	local backgroundMusic=audio.loadStream("Audio/Glow.mp3")
	local backgroundMusicChannel= audio.play( backgroundMusic, {loops=-1} )
      -- Called when the scene is now on screen.
      -- Insert code here to make the scene come alive.
      -- Example: start timers, begin animation, play audio, etc.
   end
end


-- "scene:hide()"
function scene:hide( event )
 
   local sceneGroup = self.view
   local phase = event.phase
 
		
   if ( phase == "will" ) then
      -- Called when the scene is on screen (but is about to go off screen).
      -- Insert code here to "pause" the scene.
      -- Example: stop timers, stop animation, stop audio, etc.
	    physics.stop()
		timer.cancel(timerBordi)
		timer.cancel(timerOctagon)
		timer.cancel(timerBlob)
		timer.cancel(speedTimer)
		timer.cancel(timerCountdown)
		timer.cancel (timerSpawn)
		audio.pause(backgroundMusicChannel)
		
		
   elseif ( phase == "did" ) then
      -- Called immediately after scene goes off screen.
	  
	  sceneGroup:removeSelf()
	  sceneGroup=nil
   end
end
 
-- "scene:destroy()"
function scene:destroy( event )
 
   local sceneGroup = self.view
  
end
 
---------------------------------------------------------------------------------
 
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
 
---------------------------------------------------------------------------------
 
return scene






